/**
 * 
 */
package com.shelpe.services.sinbad.service.mq.impl;

import java.util.Date;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shelpe.services.sinbad.constvalue.CacheBusinessCode.BusinessCode;
import com.shelpe.services.sinbad.controller.dto.EffectDto;
import com.shelpe.services.sinbad.controller.dto.redis.RedisDto;
import com.shelpe.services.sinbad.service.base.BaseService;
import com.shelpe.services.sinbad.service.cache.CacheService;
import com.shelpe.services.sinbad.service.mq.ShelpeMQProcessService;
import com.shelpe.services.sinbad.service.rtr.CostService;

import lombok.extern.slf4j.Slf4j;

/**
 * 针对优化的处理
 * 
 * @author louxinhua
 *
 */
@Service("kafkaMPProcessForOptimize")
@Slf4j
public class ShelpeMQProcessForOptimizeService extends BaseService implements ShelpeMQProcessService {

	/**
	 * redis缓存服务
	 */
	@Autowired
	private CacheService cacheService;
	@Autowired
	private CostService costService;

	@Override
	public void process(RedisDto dto) {
		Object obj = cacheService.getObject(BusinessCode.sync_report_optimize, String.valueOf(dto.getUserID()));
		RedisDto redisDto = null;
		boolean updateRedis = false;
		if (obj != null && obj instanceof RedisDto) {
			redisDto = (RedisDto) obj;
			long reduceMinute = (dto.getNotifyTime() - redisDto.getNotifyTime()) / (1000 * 60);
			log.debug("the user auto start sync:{} now:{} last:{} reduce:{}", dto.getUserID(), dto.getNotifyTime(),
					redisDto.getNotifyTime(), reduceMinute);
			if (reduceMinute > 120) {
				EffectDto effect = this.costService.syncBusinessCost(dto.getUserID(), 120, dto.getType());
				if (effect != null && effect.getError() == null) {
					updateRedis = true;
					redisDto.setNotifyTime(dto.getNotifyTime());
					log.debug("the user auto finish synced to taobao:{}", dto.getUserID());
				} else {
					if (effect.getError() != null && StringUtils.isNotBlank(effect.getError().getSubCode())) {
						if ("invalid-sessionkey".equals(effect.getError().getSubCode())) {
							updateRedis = true;
							redisDto.setNotifyTime(dto.getNotifyTime());
						}
					}
					log.error("the user auto failed sync to taobao:{},error:{}", dto.getUserID(), effect.getError());
				}
			} else {
				if (this.costService.findBusinessCost(dto.getUserID(), DateUtils.addMinutes(new Date(), -120), 120,
						dto.getType()) == null) {
					EffectDto effect = this.costService.syncBusinessCost(dto.getUserID(), 120, dto.getType());
					if (effect != null && effect.getError() == null) {
						updateRedis = true;
						redisDto.setNotifyTime(dto.getNotifyTime());
						log.debug("the user auto finish synced to taobao:{}", dto.getUserID());
					} else {
						if (effect.getError() != null && StringUtils.isNotBlank(effect.getError().getSubCode())) {
							if ("invalid-sessionkey".equals(effect.getError().getSubCode())) {
								updateRedis = true;
								redisDto.setNotifyTime(dto.getNotifyTime());
							}
						}
						log.error("the user auto failed sync to taobao:{},error:{}", dto.getUserID(),
								effect.getError());
					}
				}
			}
		} else {
			redisDto = new RedisDto();
			redisDto.setType(dto.getType());
			redisDto.setUserID(dto.getUserID());
			EffectDto effect = this.costService.syncBusinessCost(dto.getUserID(), 120, dto.getType());
			if (effect != null && effect.getError() == null) {
				updateRedis = true;
				redisDto.setNotifyTime(dto.getNotifyTime());
				log.debug("the user auto finish sync to taobao:{}", dto.getUserID());
			} else {
				if (effect.getError() != null && StringUtils.isNotBlank(effect.getError().getSubCode())) {
					if ("invalid-sessionkey".equals(effect.getError().getSubCode())) {
						updateRedis = true;
						redisDto.setNotifyTime(dto.getNotifyTime());
					}
				}
				log.error("the user auto failed sync to taobao:{},error:{}", dto.getUserID(), effect.getError());
			}
		}

		// 更新时间
		if (updateRedis) {
			cacheService.putObject(BusinessCode.sync_report_optimize, String.valueOf(dto.getUserID()), redisDto);
		}
	}
}
